package array.leetcode.easy;

import java.util.HashSet;
import java.util.Set;

/**
 * @author bruin_du
     * @description 数组中重复的数字
 * @date 2022/5/10 17:25
 **/
public class offer03_FindRepeatNumber {
    int choice = 1;
    public int findRepeatNumber(int[] nums) {
        //根据数组的大小和数组中的元素
        //可通过字典排序交换找到相同的元素
        if(choice == 1) {
            int i = 0;
            while (i < nums.length) {
                if (nums[i] != i) {
                    if (nums[i] == nums[nums[i]])
                        break;
                    else {
                        int tmp = nums[i];
                        nums[i] = nums[nums[i]];
                        nums[tmp] = tmp;
                    }
                } else
                    i++;
            }
            return nums[i];
        }

        //使用set
        else if(choice == 2){
            Set<Integer> set = new HashSet<>();
            for(int i = 0; i < nums.length;i++){
                if(!set.add(nums[i]))
                    return nums[i];
            }
            return -1;
        }
        return -1;
    }
}
